<template>
  <van-nav-bar title="我的收藏" left-arrow @click-left="onBack" />

  <!-- 拿到的info是数组，要遍历出来，且product里面才有商品信息 -->
  <van-card
    num="1"
    v-for="(item, index) in info"
    :key="item.id"
    :price="item.product.price"
    :title="item.product.name"
    :thumb="item.product.cover_cdn"
  >
    <template #tags>
      <van-tag plain type="primary">已有1w+人收藏</van-tag>
      <!-- <van-tag plain type="primary">标签</van-tag> -->
    </template>
    <template #footer>
      <van-button size="mini" style="color: red; border: red solid 1px" @click="onDelete(item.proid)">删除</van-button>
      <!-- <van-button size="mini">按钮</van-button> -->
    </template>
  </van-card>
</template>
<script>
  import { showNotify, Card, showConfirmDialog } from 'vant'
  export default {
    data() {
      return {
        info: {},
        proid: 0,
        LoginAuth: {},
      }
    },
    created() {
      this.LoginAuth = this.$cookies.get('LoginAuth') ? this.$cookies.get('LoginAuth') : {}

      this.getCollectionInfo()
    },
    methods: {
      onBack() {
        this.$router.push('/business/base/index')
      },
      async getCollectionInfo() {
        let data = {}

        if (this.LoginAuth.id) {
          data.busid = this.LoginAuth.id
        }
        let result = await this.$api.CollectionInfo(data)

        if (result.code === 0) {
          showNotify({
            type: 'warning',
            message: result.msg,
            duration: 1500,
            onClose: () => {
              this.$router.go(-1)
            },
          })
          return false
        }
        this.info = result.data
        // console.log(result.data)
      },
      onDelete(proid) {
        showConfirmDialog({
          title: '删除',
          message: '确认删除该收藏吗？',
        })
          .then(async () => {
            let data = {
              proid: proid,
              busid: this.LoginAuth.id,
            }
            let result = await this.$api.CollectionDelete(data)

            if (result.code === 1) {
              showNotify({
                type: 'success',
                message: result.msg,
                duration: 1500,
                onClose: () => {
                  this.$router.go(0)
                },
              })
            } else {
              showNotify({
                type: 'warning',
                message: result.msg,
                duration: 1500,
              })
            }
          })
          .catch(() => {
            // on cancel
          })
      },
    },
  }
</script>
